using UnityEngine;

//复合生成区域
public class CompositeSpawnZone : SpawnZone
{
    [SerializeField]
    SpawnZone[] spawnZones;

    [SerializeField]
    bool sequential;//是否依次遍历生成区生成形状

    [SerializeField]
    bool overrideConfig;

    int nextSequentialIndex;

    public override Vector3 SpawnPoint
    {
        get{
            int index;
            if(sequential){
                index = nextSequentialIndex++;
                if(nextSequentialIndex >= spawnZones.Length){
                    nextSequentialIndex = 0;
                }

            }else{
                index = Random.Range(0, spawnZones.Length);
            }
            
            return spawnZones[index].SpawnPoint;
        }
    }

    public override Shape SpawnShape()
    {
        if(overrideConfig)
        {
            return base.SpawnShape();
        }else{
            int index;
            if(sequential){
                index = nextSequentialIndex++;
                if(nextSequentialIndex >= spawnZones.Length){
                    nextSequentialIndex = 0;
                }

            }else{
                index = Random.Range(0, spawnZones.Length);
            }
            
            return spawnZones[index].SpawnShape();
        }
        
    }

    //保存索引
    public override void Save(GameDataWriter writer)
    {
        writer.Write(nextSequentialIndex);
    }
    public override void Load(GameDataReader reader)
    {
        nextSequentialIndex = reader.ReadInt();
    }
}